/*============================================================================
  File:    SimpleSampleAMOAccessfrm.cs

  Summary: Contains the form to execute the different steps of the sample

           Part of Simple Sample AMO Access

  Date:    November 2006
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.

  Copyright (C) Microsoft Corporation.  All rights reserved.

  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  See these other
  materials for detailed information regarding Microsoft code samples.

  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices;
using Microsoft.Samples.SqlServer;

namespace Microsoft.Samples.SqlServer
{
    public partial class SimpleSampleAMOAccessfrm : Form
    {
        private static String emptyNode = "Empty Node";
        private SimpleSampleAMOAccess simpleSample;
        public SimpleSampleAMOAccessfrm()
        {
            InitializeComponent();
            simpleSample = new SimpleSampleAMOAccess();
            processTypeForCube.SelectedIndex = processTypeForCube.FindString("ProcessFull", 0);
        }

        private void connectServer_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            connectServer.Enabled = false;
            simpleSample.ConnectServer(instanceName.Text);
            if (simpleSample.IsConnected)
            {
                instanceName.Enabled = false;
                databaseName.Enabled = false;
                datasourceName.Enabled = false;
                datasourceConnectionString.Enabled = false;
                buildStartingDB.Enabled = true;
                executeXmla.Enabled = true;
                doMining.Enabled = true;
            }

            this.Cursor = Cursors.Arrow;
        }

        private void buildStartingDB_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            buildStartingDB.Enabled = false;
            if (simpleSample.IsConnected)
            {
                startAmoTreeView(simpleSample.server);
                simpleSample.CreateStartingDatabase(simpleSample.server, databaseName.Text, datasourceName.Text, datasourceConnectionString.Text);
            }
            processDatabase.Enabled = true;
            buildStartingDB.Enabled = true;
            this.Cursor = Cursors.Arrow;
        }

        private void processDatabase_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            processDatabase.Enabled = false;
            buildStartingDB.Enabled = false;
            addFeaturesToDb.Enabled = false;
            doMining.Enabled = false;

            if (simpleSample.IsConnected)
            {
                simpleSample.ProcessDatabase(processTypeForCube.SelectedItem.ToString());
            }

            doMining.Enabled = true;
            buildStartingDB.Enabled = true;
            processDatabase.Enabled = true;
            addFeaturesToDb.Enabled = true;
            this.Cursor = Cursors.Arrow;
        }

        private void addFeaturesToDb_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            addFeaturesToDb.Enabled = false;
            processDatabase.Enabled = false;
            buildStartingDB.Enabled = false;
            doMining.Enabled = false;

            if (simpleSample.IsConnected)
            {
                simpleSample.AddFeaturesToDatabase();
                startAmoTreeView(simpleSample.server);
            }

            doMining.Enabled = true;
            buildStartingDB.Enabled = true;
            processDatabase.Enabled = true;
            addFeaturesToDb.Enabled = true;
            this.Cursor = Cursors.Arrow;

        }

        private void doMining_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            doMining.Enabled = false;
            addFeaturesToDb.Enabled = false;
            processDatabase.Enabled = false;
            buildStartingDB.Enabled = false;

            if (simpleSample.IsConnected)
            {
                simpleSample.doDatamining();
                startAmoTreeView(simpleSample.server);
            }

            buildStartingDB.Enabled = true;
            processDatabase.Enabled = true;
            addFeaturesToDb.Enabled = true;
            doMining.Enabled = true;
            this.Cursor = Cursors.Arrow;
        }

        private void disconnectFromServer_Click(object sender, EventArgs e)
        {
            connectServer.Enabled = true;
            simpleSample.DisconnectServer();
            simpleSample = new SimpleSampleAMOAccess();
            instanceName.Enabled = true;
            databaseName.Enabled = true;
            datasourceName.Enabled = true;
            datasourceConnectionString.Enabled = true;
            buildStartingDB.Enabled = false;
            processDatabase.Enabled = false;
            addFeaturesToDb.Enabled = false;
            doMining.Enabled = false;
            executeXmla.Enabled = false;
            amoPropertyGrid.SelectedObject = null;
            amoTreeView.Nodes.Clear();


        }

        private void startAmoTreeView(Server connectedServer)
        {
            amoPropertyGrid.SelectedObject = null;
            amoTreeView.Nodes.Clear();
            newNode(amoTreeView.Nodes, connectedServer.Name, connectedServer);
        }

        private void newNode(TreeNodeCollection nodes, String nodeText, Object amoObject)
        {
            TreeNode addedNode = nodes.Add(nodeText);
            addedNode.Tag = amoObject;
            addedNode.Nodes.Add(emptyNode).Tag = emptyNode;            

        }

        private void amoTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ((e == null) || (e.Node == null))
            {
                amoPropertyGrid.SelectedObject = null;
            }
            else
            {
                amoPropertyGrid.SelectedObject = e.Node.Tag;
            }
        }

        private void amoTreeView_AfterExpand(object sender, TreeViewEventArgs e)
        {
            //Check node existance first
            if ((e == null) || (e.Node == null))
            {
                return;
            }
            TreeNode startingNode = e.Node;

            //Check nodes collection is not emptyNode => collection is already populated, there is nothing else to do
            if((startingNode.Nodes.Count>1) || (startingNode.Nodes[0].Text!=emptyNode))
            {
                return;
            }

            //remove the emptyNode
            startingNode.Nodes.Clear();

            //Add new children nodes according to current node
            //To see a deeper recursive browser sample see AmoBrowser sample 


            //Expand Server object
            #region Server object
            if (startingNode.Tag is Microsoft.AnalysisServices.Server)
            {
                if (((Server)startingNode.Tag).Assemblies.Count > 0)
                {
                    newNode(startingNode.Nodes, "Assemblies", ((Server)startingNode.Tag).Assemblies);
                }

                if (((Server)startingNode.Tag).Databases.Count > 0)
                {
                    newNode(startingNode.Nodes, "Databases", ((Server)startingNode.Tag).Databases);
                }

                if (((Server)startingNode.Tag).Roles.Count > 0)
                {
                    newNode(startingNode.Nodes, "Roles", ((Server)startingNode.Tag).Roles);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.AssemblyCollection)
            {
                foreach (Assembly assembly in (AssemblyCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(assembly.Name))?assembly.ID:assembly.Name), assembly);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.DatabaseCollection)
            {
                foreach (Database db in (DatabaseCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(db.Name))?db.ID:db.Name), db);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.RoleCollection)
            {
                foreach (Role role in (RoleCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(role.Name)) ? role.ID : role.Name), role);
                }
                return;
            }

            #endregion

            //Expand Database object
            #region Database object
            if (startingNode.Tag is Microsoft.AnalysisServices.Database)
            {
                if (((Database)startingNode.Tag).DataSources.Count > 0)
                {
                    newNode(startingNode.Nodes, "DataSources", ((Database)startingNode.Tag).DataSources);
                }

                if (((Database)startingNode.Tag).DataSourceViews.Count > 0)
                {
                    newNode(startingNode.Nodes, "DataSourceViews", ((Database)startingNode.Tag).DataSourceViews);
                }

                if (((Database)startingNode.Tag).Dimensions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Dimensions", ((Database)startingNode.Tag).Dimensions);
                }

                if (((Database)startingNode.Tag).Cubes.Count > 0)
                {
                    newNode(startingNode.Nodes, "Cubes", ((Database)startingNode.Tag).Cubes);
                }

                if (((Database)startingNode.Tag).MiningStructures.Count > 0)
                {
                    newNode(startingNode.Nodes, "MiningStructures", ((Database)startingNode.Tag).MiningStructures);
                }

                if (((Database)startingNode.Tag).DatabasePermissions.Count > 0)
                {
                    newNode(startingNode.Nodes, "DatabasePermissions", ((Database)startingNode.Tag).DatabasePermissions);
                }

                if (((Database)startingNode.Tag).Roles.Count > 0)
                {
                    newNode(startingNode.Nodes, "Roles", ((Database)startingNode.Tag).Roles);
                }

                if (((Database)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((Database)startingNode.Tag).Translations);
                }

                if (((Database)startingNode.Tag).Assemblies.Count > 0)
                {
                    newNode(startingNode.Nodes, "Assemblies", ((Database)startingNode.Tag).Assemblies);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.DataSourceCollection)
            {
                foreach (DataSource ds in (DataSourceCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ds.Name)) ? ds.ID : ds.Name), ds);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.DataSourceViewCollection)
            {
                foreach (DataSourceView dsv in (DataSourceViewCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(dsv.Name)) ? dsv.ID : dsv.Name), dsv);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.DimensionCollection)
            {
                foreach (Dimension dim in (DimensionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(dim.Name)) ? dim.ID : dim.Name), dim);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.CubeCollection)
            {
                foreach (Cube cube in (CubeCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(cube.Name)) ? cube.ID : cube.Name), cube);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MiningStructureCollection)
            {
                foreach (MiningStructure ms in (MiningStructureCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ms.Name)) ? ms.ID : ms.Name), ms);
                }
                return;
            }

            
            if (startingNode.Tag is Microsoft.AnalysisServices.DatabasePermissionCollection)
            {
                foreach (DatabasePermission dp in (DatabasePermissionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(dp.Name)) ? dp.ID : dp.Name), dp);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.TranslationCollection)
            {
                foreach (Translation tr in (TranslationCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(tr.Description)) ? tr.Language.ToString() : tr.Description), tr);
                }
                return;
            }


            #endregion

            //Expand Dimension object
            #region Dimension object
            if (startingNode.Tag is Microsoft.AnalysisServices.Dimension)
            {
                if (((Dimension)startingNode.Tag).Attributes.Count > 0)
                {
                    newNode(startingNode.Nodes, "Attributes", ((Dimension)startingNode.Tag).Attributes);
                }

                if (((Dimension)startingNode.Tag).Hierarchies.Count > 0)
                {
                    newNode(startingNode.Nodes, "Hierarchies", ((Dimension)startingNode.Tag).Hierarchies);
                }

                if (((Dimension)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((Dimension)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.DimensionAttributeCollection)
            {
                foreach (DimensionAttribute da in (DimensionAttributeCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(da.Name)) ? da.ID : da.Name), da);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.HierarchyCollection)
            {
                foreach (Hierarchy h in (HierarchyCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(h.Name)) ? h.ID : h.Name), h);
                }
                return;
            }
            #endregion

            //Expand Hierarchy object
            #region Hierarchy object
            if (startingNode.Tag is Microsoft.AnalysisServices.Hierarchy)
            {
                if (((Hierarchy)startingNode.Tag).Levels.Count > 0)
                {
                    newNode(startingNode.Nodes, "Levels", ((Hierarchy)startingNode.Tag).Levels);
                }

                if ((( Hierarchy)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", (( Hierarchy)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.LevelCollection)
            {
                foreach (Level lc in (LevelCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(lc.Name)) ? lc.ID : lc.Name), lc);
                }
                return;
            }
            #endregion

            //Expand Cube object
            #region Cube object
            if (startingNode.Tag is Microsoft.AnalysisServices.Cube)
            {
                if (((Cube)startingNode.Tag).Actions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Actions", ((Cube)startingNode.Tag).Actions);
                }

                if (((Cube)startingNode.Tag).Dimensions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Dimensions", ((Cube)startingNode.Tag).Dimensions);
                }

                if (((Cube)startingNode.Tag).Kpis.Count > 0)
                {
                    newNode(startingNode.Nodes, "Kpis", ((Cube)startingNode.Tag).Kpis);
                }

                if (((Cube)startingNode.Tag).MdxScripts.Count > 0)
                {
                    newNode(startingNode.Nodes, "MdxScripts", ((Cube)startingNode.Tag).MdxScripts);
                }

                if (((Cube)startingNode.Tag).MeasureGroups.Count > 0)
                {
                    newNode(startingNode.Nodes, "MeasureGroups", ((Cube)startingNode.Tag).MeasureGroups);
                }

                if (((Cube)startingNode.Tag).Perspectives.Count > 0)
                {
                    newNode(startingNode.Nodes, "Perspectives", ((Cube)startingNode.Tag).Perspectives);
                }

                if (((Cube)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((Cube)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.ActionCollection)
            {
                foreach (Action a in (ActionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(a.Name)) ? a.ID : a.Name), a);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.CubeDimensionCollection)
            {
                foreach (CubeDimension cd in (CubeDimensionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(cd.Name)) ? cd.ID : cd.Name), cd);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.KpiCollection)
            {
                foreach (Kpi kpi in (KpiCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(kpi.Name)) ? kpi.ID : kpi.Name), kpi);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MdxScriptCollection)
            {
                foreach (MdxScript mdx in (MdxScriptCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(mdx.Name)) ? mdx.ID : mdx.Name), mdx);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MeasureGroupCollection)
            {
                foreach (MeasureGroup mg in (MeasureGroupCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(mg.Name)) ? mg.ID : mg.Name), mg);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveCollection)
            {
                foreach (Perspective p in (PerspectiveCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(p.Name)) ? p.ID : p.Name), p);
                }
                return;
            }

            #endregion

            //Expand CubeDimension object
            #region CubeDimension object
            if (startingNode.Tag is Microsoft.AnalysisServices.CubeDimension)
            {
                if (((CubeDimension)startingNode.Tag).Attributes.Count > 0)
                {
                    newNode(startingNode.Nodes, "Attributes", ((CubeDimension)startingNode.Tag).Attributes);
                }

                if (((CubeDimension)startingNode.Tag).Hierarchies.Count > 0)
                {
                    newNode(startingNode.Nodes, "Hierarchies", ((CubeDimension)startingNode.Tag).Hierarchies);
                }

                if (((CubeDimension)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((CubeDimension)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.CubeAttributeCollection)
            {
                foreach (CubeAttribute ca in (CubeAttributeCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ca.Attribute.Name)) ? ca.AttributeID : ca.Attribute.Name), ca);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.CubeHierarchyCollection)
            {
                foreach (CubeHierarchy ch in (CubeHierarchyCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ch.Hierarchy.Name)) ? ch.HierarchyID : ch.Hierarchy.Name), ch);
                }
                return;
            }
            #endregion

            //Expand MeasureGroup object
            #region MeasureGroup object
            if (startingNode.Tag is Microsoft.AnalysisServices.MeasureGroup)
            {
                if (((MeasureGroup)startingNode.Tag).AggregationDesigns.Count > 0)
                {
                    newNode(startingNode.Nodes, "AggregationDesigns", ((MeasureGroup)startingNode.Tag).AggregationDesigns);
                }

                if (((MeasureGroup)startingNode.Tag).Dimensions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Dimensions", ((MeasureGroup)startingNode.Tag).Dimensions);
                }

                if (((MeasureGroup)startingNode.Tag).Measures.Count > 0)
                {
                    newNode(startingNode.Nodes, "Measures", ((MeasureGroup)startingNode.Tag).Measures);
                }

                if (((MeasureGroup)startingNode.Tag).Partitions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Partitions", ((MeasureGroup)startingNode.Tag).Partitions);
                }

                if (((MeasureGroup)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((MeasureGroup)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.AggregationDesignCollection)
            {
                foreach (AggregationDesign ad in (AggregationDesignCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ad.Name)) ? ad.ID : ad.Name), ad);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MeasureGroupDimensionCollection)
            {
                foreach (MeasureGroupDimension mgd in (MeasureGroupDimensionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(mgd.CubeDimension.Name)) ? mgd.CubeDimensionID : mgd.CubeDimension.Name), mgd);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MeasureCollection)
            {
                foreach (Measure m in (MeasureCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(m.Name)) ? m.ID : m.Name), m);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PartitionCollection)
            {
                foreach (Partition p in (PartitionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(p.Name)) ? p.ID : p.Name), p);
                }
                return;
            }
            #endregion

            //Expand AggregationDesign object
            #region AggregationDesign object
            if (startingNode.Tag is Microsoft.AnalysisServices.AggregationDesign)
            {
                if (((AggregationDesign)startingNode.Tag).Aggregations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Aggregations", ((AggregationDesign)startingNode.Tag).Aggregations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.AggregationCollection)
            {
                foreach (Aggregation a in (AggregationCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(a.Name)) ? a.ID : a.Name), a);
                }
                return;
            }
            #endregion

            //Expand Aggregation object
            #region Aggregation object
            if (startingNode.Tag is Microsoft.AnalysisServices.Aggregation)
            {
                if (((Aggregation)startingNode.Tag).Dimensions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Dimensions", ((Aggregation)startingNode.Tag).Dimensions);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.AggregationDimensionCollection)
            {
                foreach (AggregationDimension ad in (AggregationDimensionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ad.CubeDimension.Name)) ? ad.CubeDimensionID : ad.CubeDimension.Name), ad);
                }
                return;
            }
            #endregion

            //Expand Perspective object
            #region Perspective object
            if (startingNode.Tag is Microsoft.AnalysisServices.Perspective)
            {
                if (((Perspective)startingNode.Tag).Actions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Actions", ((Perspective)startingNode.Tag).Actions);
                }

                if (((Perspective)startingNode.Tag).Calculations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Calculations", ((Perspective)startingNode.Tag).Calculations);
                }

                if (((Perspective)startingNode.Tag).Dimensions.Count > 0)
                {
                    newNode(startingNode.Nodes, "Dimensions", ((Perspective)startingNode.Tag).Dimensions);
                }

                if (((Perspective)startingNode.Tag).Kpis.Count > 0)
                {
                    newNode(startingNode.Nodes, "Kpis", ((Perspective)startingNode.Tag).Kpis);
                }

                if (((Perspective)startingNode.Tag).MeasureGroups.Count > 0)
                {
                    newNode(startingNode.Nodes, "MeasureGroups", ((Perspective)startingNode.Tag).MeasureGroups);
                }

                if (((Perspective)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((Perspective)startingNode.Tag).Translations);
                }

                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveActionCollection)
            {
                foreach (PerspectiveAction pa in (PerspectiveActionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pa.Action.Name)) ? pa.ActionID : pa.Action.Name), pa);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveCalculationCollection)
            {
                foreach (PerspectiveCalculation pc in (PerspectiveCalculationCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pc.Name)) ? pc.ToString() : pc.Name), pc);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveDimensionCollection)
            {
                foreach (PerspectiveDimension pd in (PerspectiveDimensionCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pd.CubeDimension.Name)) ? pd.CubeDimensionID : pd.CubeDimension.Name), pd);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveKpiCollection)
            {
                foreach (PerspectiveKpi pk in (PerspectiveKpiCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pk.Kpi.Name)) ? pk.KpiID : pk.Kpi.Name), pk);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveMeasureGroupCollection)
            {
                foreach (PerspectiveMeasureGroup pmg in (PerspectiveMeasureGroupCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pmg.MeasureGroup.Name)) ? pmg.MeasureGroupID : pmg.MeasureGroup.Name), pmg);
                }
                return;
            }
            #endregion

            //Expand PerspectiveMeasureGroup object
            #region PerspectiveMeasureGroup object
            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveMeasureGroup)
            {
                if (((PerspectiveMeasureGroup)startingNode.Tag).Measures.Count > 0)
                {
                    newNode(startingNode.Nodes, "Measures", ((PerspectiveMeasureGroup)startingNode.Tag).Measures);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.PerspectiveMeasureCollection)
            {
                foreach (PerspectiveMeasure pm in (PerspectiveMeasureCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(pm.Measure.Name)) ? pm.MeasureID : pm.Measure.Name), pm);
                }
                return;
            }
            #endregion

            //Expand MiningStructure object
            #region MiningStructure object
            if (startingNode.Tag is Microsoft.AnalysisServices.MiningStructure)
            {
                if (((MiningStructure)startingNode.Tag).Columns.Count > 0)
                {
                    newNode(startingNode.Nodes, "Columns", ((MiningStructure)startingNode.Tag).Columns);
                }

                if (((MiningStructure)startingNode.Tag).MiningModels.Count > 0)
                {
                    newNode(startingNode.Nodes, "MiningModels", ((MiningStructure)startingNode.Tag).MiningModels);
                }

                if (((MiningStructure)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((MiningStructure)startingNode.Tag).Translations);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MiningStructureColumnCollection)
            {
                foreach (MiningStructureColumn msc in (MiningStructureColumnCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(msc.Name)) ? msc.ID : msc.Name), msc);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.MiningModelCollection)
            {
                foreach (MiningModel mm in (MiningModelCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(mm.Name)) ? mm.ID : mm.Name), mm);
                }
                return;
            }
            #endregion

            //Expand MiningModel object
            #region MiningModel object
            if (startingNode.Tag is Microsoft.AnalysisServices.MiningModel)
            {
                if (((MiningModel)startingNode.Tag).AlgorithmParameters.Count > 0)
                {
                    newNode(startingNode.Nodes, "AlgorithmParameters", ((MiningModel)startingNode.Tag).AlgorithmParameters);
                }

                if (((MiningModel)startingNode.Tag).Translations.Count > 0)
                {
                    newNode(startingNode.Nodes, "Translations", ((MiningModel)startingNode.Tag).Translations);
                }
                return;
            }

            if (startingNode.Tag is Microsoft.AnalysisServices.AlgorithmParameterCollection)
            {
                foreach (AlgorithmParameter ap in (AlgorithmParameterCollection)startingNode.Tag)
                {
                    newNode(startingNode.Nodes, ((String.IsNullOrEmpty(ap.Name)) ? ap.ToString() : ap.Name), ap);
                }
                return;
            }
            #endregion
            return;
        }

        private void executeXmla_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder results = new StringBuilder();
                XmlaResultCollection xmlaExecuteResults = simpleSample.server.Execute(xmlaCommand.Text);
                foreach (XmlaResult xmlaResult in xmlaExecuteResults)
                {
                    StringBuilder messages = new StringBuilder();
                    messages.AppendLine();
                    foreach (XmlaMessage xmlaMessage in xmlaResult.Messages)
                    {
                        messages.AppendLine(string.Concat("\t", xmlaMessage.Description, ". [", xmlaMessage.Source, ":", xmlaMessage.Location, "]"));
                    }
                    results.AppendLine(string.Concat(xmlaResult.Value, messages.ToString()));
                }
                XmlaExecuteResultsForm xmlaExecuteResultsForm = new XmlaExecuteResultsForm();
                String executionResults = results.ToString().Trim();
                xmlaExecuteResultsForm.Controls["xmlaExecuteResultsText"].Text = String.IsNullOrEmpty(executionResults)?"Execution completed with no results":executionResults;
                xmlaExecuteResultsForm.Visible = true;
                xmlaExecuteResultsForm.Activate();
                xmlaExecuteResultsForm.TopMost = true;

            }
            catch (AmoException amoException)
            {
                MessageBox.Show(string.Concat("ERROR executing XMLA command: ", amoException.Message));
            }

        }
        


    }
}